﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Extension;
using System;
[AddComponentMenu("UI/SubjectiveSlider")]
public class SubjectiveSlider : Slider
{
    [SerializeField]
    ControlSubject m_valueSubject = new ControlSubject(false, typeof(int), typeof(float)), 
        m_maxValueSubject = new ControlSubject(false, typeof(int), typeof(float));

    public bool m_animate = false;
    public float m_animationSpeed = 1f, targetValue = 0;


    public ControlSubject valuesubject { get { return m_valueSubject; } }
    public ControlSubject maxvaluesubject { get { return m_maxValueSubject; } }

    protected override void OnEnable()
    {
        base.OnEnable();
        AssignValue();

        fillRect.offsetMin = new Vector2(0, 0);
        fillRect.offsetMax = new Vector2(0, 0);
    }

    protected override void Start()
    {
        base.Start();
        AssignValue();
    }

    private void AssignValue()
    {
        if (valuesubject.hasContent)
            targetValue = (float)valuesubject.value;
        if (maxvaluesubject.hasContent)
            maxValue = (float)maxvaluesubject.value;
    }

    void Update()
    {
        value = Mathf.Lerp(value, targetValue, Time.deltaTime * m_animationSpeed);
    }

    void LateUpdate()
    {
        if(valuesubject.hasUpdate || maxvaluesubject.hasUpdate)
            AssignValue();
    }

#if UNITY_EDITOR

    protected override void OnValidate()
    {
        base.OnValidate();
        AssignValue();
    }
#endif
}
